<!--
 * @Description: 商品详情页面组件
 -->
<template>
  <div id="details">
    <!-- 头部 -->
    <div class="page-header">
      <div class="title">
        <p>{{ productDetails.good_name }}</p>
        <div class="list">
          <ul>
            <li>
              <router-link to>概述</router-link>
            </li>
            <li>
              <router-link to>参数</router-link>
            </li>
            <li>
              <!-- <router-link to @click="evaluate(productID)">用户评价</router-link> -->
              <div @click="select_evaluate(productID)" class="aaa">
                用户评价
              </div>
            </li>
          </ul>
        </div>
      </div>
    </div>
    <!-- 头部END -->

    <!-- 主要内容 -->
    <div class="main">
      <!-- 左侧商品轮播图 -->
      <div class="block">
        <el-carousel height="560px" v-if="productPicture.length > 1">
          <el-carousel-item v-for="item in productPicture" :key="item.id">
            <img
              style="height:560px;"
              :src="$target + item.img"
              :alt="item.id"
            />
          </el-carousel-item>
        </el-carousel>
        <div v-if="productPicture.length == 1">
          <img
            style="height:560px;"
            :src="$target + productPicture[0].product_picture"
            :alt="productPicture[0].intro"/>
        </div>
      </div>
      <!-- 左侧商品轮播图END -->

      <!-- 右侧内容区 -->
      <div class="content">
        <h1 class="name">{{ productDetails.good_name }}</h1>
        <p class="intro">{{ productDetails.decribe }}</p>
        <!-- <p class="store">小米自营</p> -->
        <p class="store">{{ productDetails.saler }}</p>
        <div class="price">
          <span
            >{{
              productDetails.goodsale_price *
                (productDetails.goods_discount / 100)
            }}元</span
          >
          <span v-show="productDetails.goods_discount != 100" class="del"
            >{{ productDetails.goodsale_price }}元</span
          >
        </div>
        <div class="pro-list">
          <span class="pro-name">{{ productDetails.good_name }}</span>
          <span class="pro-price">
            <!-- <span>{{productDetails.goodsale_price * (productDetails.goods_discount / 100)}}元</span>
            <span
              v-show="productDetails.goods_discount != 100"
              class="del"
            >{{productDetails.goodsale_price}}元</span> -->
          </span>
          <p v-if="productDetails.seckil_discount > 0" class="price-sum">总计 : {{productDetails.goodsale_price * (productDetails.seckil_discount / 100)}}元</p>
          <p v-else class="price-sum">总计 : {{productDetails.goodsale_price * (productDetails.goods_discount / 100)}}元</p>
        </div>
        <!-- 内容区底部按钮 -->
        <div class="button">
          <el-button class="shop-cart" :disabled="dis" @click="addShoppingCart"
            >加入购物车</el-button
          >
          <el-button class="like" @click="addCollect">{{ res }}</el-button>
        </div>
        <!-- 内容区底部按钮END -->
        <div class="pro-policy">
          <ul>
            <li><em class="el-icon-circle-check"></em> 贵美自营</li>
            <li><em class="el-icon-circle-check"></em> 贵美发货</li>
            <li><em class="el-icon-circle-check"></em> 7天无理由退货</li>
            <li><em class="el-icon-circle-check"></em> 7天价格保护</li>
          </ul>
        </div>
      </div>
      <!-- 右侧内容区END -->
    </div>
    <!-- 主要内容END -->

    <!-- 弹出框 -->

    <el-drawer
      title="商品评价"
      :visible.sync="tanev"
      width="30%"
      direction="ltr"
      class="el-drawer__body"
    >


      <el-card class="box-card" v-for="item in evaluate_content" :key="item" >
        <!-- <ul v-infinite-scroll="load"> -->
        <div slot="header" class="clearfix">
          <span style="color:green">{{item.user_name}}</span>
          <!-- <el-button style="float: right; padding: 3px 0" type="text"
            >操作按钮</el-button
          > -->
        </div>
        <div  class="text item">
          {{ item.content }}
        </div>
        <!-- </ul> -->
      </el-card>
    </el-drawer>
    <!-- 弹出框 END -->
  </div>
</template>
<script>
import { mapActions } from "vuex";
import { getGoodsDetailById } from '../api/godsDetail';
import { getCollectionById } from '../api/godsDetail';
import { getBrowseById } from '../api/godsDetail';
import { selectEvaluate } from "../api/evaluate.js";
export default {
  data() {
    return {
      dis: false, // 控制“加入购物车按钮是否可用”
      productID: "", // 商品id
      productDetails: {}, // 商品详细信息
      productPicture: [], // 商品图片
      res: {},
      evaluate_content: [],
      tanev: false,
      count: 0,
    };
  },
  // 通过路由获取商品id
  activated() {
    if (this.$route.query.productID != undefined) {
      this.productID = this.$route.query.productID;
      console.log(this.productID);
    }
  },
  watch: {
    // 监听商品id的变化，请求后端获取商品数据
    productID: function(val) {
      this.getDetails(val);

      // this.getDetailsPicture(val);
    },
  },
  methods: {
    select_evaluate(productID) {
      console.log(productID);
      var data = {
        good_id: productID,
      };

      selectEvaluate(data).then((res) => {
        console.log("`````````````````````````");
        console.log(res);
        this.evaluate_content = res;
        console.log("~~~~~~~~~~~~~~~~~~~~~");
        console.log(res);
        this.tanev = true;
      });
    },

    ...mapActions([
      "unshiftShoppingCart",
      "addShoppingCartNum",
      "setShoppingCart",
    ]),
    // 获取商品详细信息
    getDetails(val) {
      var data = {
        good_id: val
      }
      var data2 = {
        id:this.$store.getters.getUser.id,
        good_id:val
      }
      // console.log(data);
      /**
       * 请求商品详细信息
       */
      getGoodsDetailById(data).then((res) => {
        console.log(res);
        this.productDetails = res;
        this.productPicture = [];
        // 生成图片数组
        for (let i = 1; i <= 5; i++) {
          /**
           *  `` 可以写表达式
           */
          if (this.productDetails[`small_pic${i}`]) {
            let img = {
              id: i,
              img: this.productDetails[`small_pic${i}`],
            };
            this.productPicture.push(img);
          }
          // console.log(this.productPicture);
        }
      })

      /**
       * 判断是否收藏
       */
      getCollectionById(data2)
        .then(res=>{
           console.log(res);
          this.res=res.mes;
        })
         /**
         添加浏览记录
         */
        getBrowseById(data2)
        .then(res=>{
          console.log(111);
          console.log(res.mes);
        })



    },
    // 加入购物车
    addShoppingCart() {
      // 判断是否登录,没有登录则显示登录组件
      if (!this.$store.getters.getUser) {
        this.$store.dispatch("setShowLogin", true);
        return;
      }
      this.$axios
        .post("http://localhost:8080/shop/cart/add", {
          user_id: this.$store.getters.getUser.id,
          goods_id: this.productID,
        })
        .then(res => {
          switch(res.data) {
            case "添加成功1":
              // 新加入购物车成功
              // console.log(res.data)
              /**
               * 更新购物车
               */
              this.$axios
                .post(
                  "http://localhost:8080/shop/cart/find",
                  {
                    user_id: this.$store.getters.getUser.id,
                  }
                  // {'Content-Type': 'application/json'}
                )
                .then((res) => {
                  this.setShoppingCart(res.data);
                })
                .catch((err) => {
                  return Promise.reject(err);
                });

              this.notifySucceed("商品添加购物车成功");
              break;
            // case "002":
            //   // 该商品已经在购物车，数量+1
            //   this.addShoppingCartNum(this.productID);
            //   this.notifySucceed(res.data.msg);
            //   break;
            // case "003":
            //   // 商品数量达到限购数量
            //   this.dis = true;
            //   this.notifyError(res.data.msg);
            //   break;
            default:
              this.notifyError(res.data);
          }
        })
        .catch((err) => {
          return Promise.reject(err);
        });
    },
    // 添加收藏
    addCollect() {
      // 判断是否登录,没有登录则显示登录组件
      if (!this.$store.getters.getUser) {
        this.$store.dispatch("setShowLogin", true);
        return;
      }
      console.log(this.res);
      if (this.res == "未收藏") {
        this.$axios
          .post("http://localhost:8080/shop/addcollection", {
            id: this.$store.getters.getUser.id,
            good_id: this.productID,
          })
          .then((res) => {
            if (res.data.mes == "收藏成功") {
              // 添加收藏成功
              console.log(this.$store.getters.getUser.id);
              console.log(this.productID);
              // this.notifySucceed(res.data.msg);
              this.getDetails(this.productID);
            } else {
              // 添加收藏失败
              this.notifyError(res.data.msg);
            }
          })
          .catch((err) => {
            return Promise.reject(err);
          });
      } else {
        this.$axios
          .post("http://localhost:8080/shop/deletecollection", {
            id: this.$store.getters.getUser.id,
            good_id: this.productID,
          })
          .then((res) => {
            switch (res.data.mes) {
              case "删除成功":
                // 删除成功
                // 删除删除列表中的该商品信息
                // for (let i = 0; i < this.list.length; i++) {
                //   const temp = this.list[i];
                //   if (temp.good_id == good_id) {
                //     this.list.splice(i, 1);
                //   }
                // }
                // 提示删除成功信息
                // this.notifySucceed(res.data.msg);
                this.getDetails(this.productID);
                break;
              default:
                // 提示删除失败信息
                this.notifyError(res.data.msg);
            }
          })
          .catch((err) => {
            return Promise.reject(err);
          });
      }
    },
  },
};
</script>
<style>

.el-drawer__body {
    overflow: auto;
    /* overflow-x: auto; */
}

/* 头部CSS */
#details .page-header {
  height: 64px;
  margin-top: -20px;
  z-index: 4;
  background: #fff;
  border-bottom: 1px solid #e0e0e0;
  -webkit-box-shadow: 0px 5px 5px rgba(0, 0, 0, 0.07);
  box-shadow: 0px 5px 5px rgba(0, 0, 0, 0.07);
}
#details .page-header .title {
  width: 1225px;
  height: 64px;
  line-height: 64px;
  font-size: 18px;
  font-weight: 400;
  color: #212121;
  margin: 0 auto;
}
#details .page-header .title p {
  float: left;
}
#details .page-header .title .list {
  height: 64px;
  float: right;
}
#details .page-header .title .list li {
  float: left;
  margin-left: 20px;
}
#details .page-header .title .list li a {
  font-size: 14px;
  color: #616161;
}
#details .page-header .title .list li a:hover {
  font-size: 14px;
  color: #ff6700;
}
.aaa {
  font-size: 14px;
  color: #616161;
}
.aaa:hover {
  color: #ff6700;
}
/* 头部CSS END */

/* 主要内容CSS */
#details .main {
  width: 1225px;
  height: 560px;
  padding-top: 30px;
  margin: 0 auto;
}
#details .main .block {
  float: left;
  width: 560px;
  height: 560px;
  /* 图片居中 */
  display: table-cell; /*主要是这个属性*/
  vertical-align: middle;
  text-align: center;
}
/* #details .main .block img{
  
} */
#details .el-carousel .el-carousel__indicator .el-carousel__button {
  background-color: rgba(163, 163, 163, 0.8);
}
#details .main .content {
  float: left;
  margin-left: 25px;
  width: 640px;
}
#details .main .content .name {
  height: 30px;
  line-height: 30px;
  font-size: 24px;
  font-weight: normal;
  color: #212121;
}
#details .main .content .intro {
  color: #b0b0b0;
  padding-top: 10px;
}
#details .main .content .store {
  color: #ff6700;
  padding-top: 10px;
}
#details .main .content .price {
  display: block;
  font-size: 18px;
  color: #ff6700;
  border-bottom: 1px solid #e0e0e0;
  padding: 25px 0 25px;
}
#details .main .content .price .del {
  font-size: 14px;
  margin-left: 10px;
  color: #b0b0b0;
  text-decoration: line-through;
}
#details .main .content .pro-list {
  background: #f9f9fa;
  padding: 30px 60px;
  margin: 50px 0 50px;
}
#details .main .content .pro-list span {
  line-height: 30px;
  color: #616161;
}
#details .main .content .pro-list .pro-price {
  float: right;
}
#details .main .content .pro-list .pro-price .pro-del {
  margin-left: 10px;
  text-decoration: line-through;
}
#details .main .content .pro-list .price-sum {
  color: #ff6700;
  font-size: 24px;
  padding-top: 20px;
}
#details .main .content .button {
  height: 55px;
  margin: 10px 0 20px 0;
}
#details .main .content .button .el-button {
  float: left;
  height: 55px;
  font-size: 16px;
  color: #fff;
  border: none;
  text-align: center;
}
#details .main .content .button .shop-cart {
  width: 340px;
  background-color: #ff6700;
}
#details .main .content .button .shop-cart:hover {
  background-color: #f25807;
}

#details .main .content .button .like {
  width: 260px;
  margin-left: 40px;
  background-color: #b0b0b0;
}
#details .main .content .button .like:hover {
  background-color: #757575;
}
#details .main .content .pro-policy li {
  float: left;
  margin-right: 20px;
  color: #b0b0b0;
}
/* 主要内容CSS END */
</style>
